package com.example.pingxue.init;

import com.example.pingxue.entity.*;
import com.example.pingxue.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class DataInitializer implements CommandLineRunner {
    private final StudentRepository studentRepository;
    private final TeacherRepository teacherRepository;
    private final CourseRepository courseRepository;
    private final ClassRepository classRepository;
    private final SelectionRepository selectionRepository;

    @Override
    public void run(String... args) throws Exception {
        // 创建课程
        Course math = new Course();
        math.setCourseName("高等数学");
        math.setCreditHours(64);
        math.setCredit("4.0");
        courseRepository.save(math);

        Course java = new Course();
        java.setCourseName("Java编程基础");
        java.setCreditHours(48);
        java.setCredit("3.0");
        courseRepository.save(java);

        // 创建教师
        Teacher teacherLi = new Teacher();
        teacherLi.setTeacherName("李老师");
        teacherLi.setSchool("计算机学院");
        teacherLi.setResearch("软件工程");
        teacherLi.setUsername("teacherli");
        teacherLi.setPassword("123456");
        teacherRepository.save(teacherLi);

        Teacher teacherWang = new Teacher();
        teacherWang.setTeacherName("王老师");
        teacherWang.setSchool("数学学院");
        teacherWang.setResearch("应用数学");
        teacherWang.setUsername("teacherwang");
        teacherWang.setPassword("123456");
        teacherRepository.save(teacherWang);

        // 创建班级
        TClass mathClass1 = new TClass();
        mathClass1.setTTime("周一 1-2节");
        mathClass1.setTWeek("1-16周");
        mathClass1.setRoom("教1-101");
        mathClass1.setCapacity(50);
        mathClass1.setCourse(math);
        mathClass1.setTeacher(teacherWang);
        classRepository.save(mathClass1);

        TClass javaClass1 = new TClass();
        javaClass1.setTTime("周三 3-4节");
        javaClass1.setTWeek("1-16周");
        javaClass1.setRoom("教2-202");
        javaClass1.setCapacity(40);
        javaClass1.setCourse(java);
        javaClass1.setTeacher(teacherLi);
        classRepository.save(javaClass1);

        // 创建学生
        Student studentZhang = new Student();
        studentZhang.setStudentName("张三");
        studentZhang.setMajor("计算机科学与技术");
        studentZhang.setGrade("2021级");
        studentZhang.setSchool("计算机学院");
        studentZhang.setUsername("zhang3");
        studentZhang.setPassword("123456");
        studentRepository.save(studentZhang);

        Student studentLi = new Student();
        studentLi.setStudentName("李四");
        studentLi.setMajor("软件工程");
        studentLi.setGrade("2021级");
        studentLi.setSchool("计算机学院");
        studentLi.setUsername("li4");
        studentLi.setPassword("123456");
        studentRepository.save(studentLi);

        Student studentWang = new Student();
        studentWang.setStudentName("王五");
        studentWang.setMajor("数学与应用数学");
        studentWang.setGrade("2022级");
        studentWang.setSchool("数学学院");
        studentWang.setUsername("wang5");
        studentWang.setPassword("123456");
        studentRepository.save(studentWang);

        // 创建选课记录
        Selection s1 = new Selection(studentZhang, mathClass1);
        Selection s2 = new Selection(studentZhang, javaClass1);
        Selection s3 = new Selection(studentLi, javaClass1);
        Selection s4 = new Selection(studentWang, mathClass1);
        selectionRepository.saveAll(List.of(s1, s2, s3, s4));
    }

}
